/*
Source : https://leetcode.com/problems/reverse-integer/
Author : nflush@outlook.com
Date   : 2016-05-6
*/
/*
3. Longest Substring Without Repeating Characters  
Total Accepted: 145648 Total Submissions: 663013 Difficulty: Medium

Given a string, find the length of the longest substring without repeating characters.

Examples:

Given "abcabcbb", the answer is "abc", which the length is 3.

Given "bbbbb", the answer is "b", with the length of 1.

Given "pwwkew", the answer is "wke", with the length of 3. Note that the answer must be a substring, "pwke" is a subsequence and not a substring.

Subscribe to see which companies asked this question
Have you met this question in a real interview?

*/

int lengthOfLongestSubstring(char *s)
{
    int checkBox[256] = {0};
    int maxLen = 0;
    int curLen = 0;
    int startPoint = 0;
    int curPoint;
    for(curPoint = 1; *s; s++, curPoint++) {
        char ch = *s;
        if(checkBox[ch] <= startPoint) {
            curLen = curPoint - startPoint;
            if(curLen > maxLen) {
                maxLen = curLen;
            }
        } else {
            curLen = curPoint - checkBox[ch];
            startPoint = checkBox[ch];
        }
        checkBox[ch] = curPoint;
    }
    return maxLen;
}

class Solution
{
public:
    int lengthOfLongestSubstring(string s)
    {
        int checkBox[256] = {0};
        int maxLen = 0;
        int curLen = 0;
        int startPoint = 0;
        int curPoint = 1;
        for(char ch : s) {
            if(checkBox[ch] <= startPoint) {
                curLen = curPoint - startPoint;
                if(curLen > maxLen) {
                    maxLen = curLen;
                }
            } else {
                curLen = curPoint - checkBox[ch];
                startPoint = checkBox[ch];
            }
            checkBox[ch] = curPoint;
            curPoint++;
        }
        return maxLen;
    }
};
